﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Fyp.Models;
using WebMatrix.WebData;
using System.Web.Security;

namespace Fyp.Controllers
{
    public class ReserveProjectController : Controller
    {
        //
        // GET: /ReserveProject/

        FYPEntities5 db = new FYPEntities5();

        public ActionResult StudentView()
        {
            return View();

        }
        public ActionResult SearchProject()
        {
            return View();
        }
        [HttpPost]
        public ActionResult SearchProject(FormCollection c)
        {
            var word = c["search"];
            var db = new FYPEntities5();
            try
            {
                IQueryable<Project> q = from ms in db.Projects
                                        where ms.Title.Contains(word)
                                        select ms;
                ViewData["data"] = q.ToList();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.InnerException);
            }
            return View("SearchProject");
        }
        [HttpPost]
        public ActionResult RequestList(FormCollection c)
        {
            //List<Plist> mylist = new List<Plist>();
            // var mystu_list = new List<Slist>();
            int getcateId = int.Parse(c["GroupField"]);
            global.pid_admin = getcateId;

            var q1 = from p in db.Projects
                     from g in db.Groups

                     where getcateId == g.GId && g.PId == p.PId
                     select new
                     {
                         p.Title,
                         p.Status,
                         g.GId,
                         p.AdvisorName,


                     };
            foreach (var stp in q1)
            {
                //Plist mypro = new Plist();
                // Slist std = new Slist();
                ViewBag.PTitle = stp.Title;
                ViewBag.GId = stp.GId;
                ViewBag.Advisor = stp.AdvisorName;


                ViewBag.status = stp.Status;
                //     std.StudentName = stp.Std_Name;
                //    std.StudentRollNo=stp.Std_RollNo;
                //   std.StudentEmail = stp.Std_Email;



                //mypro.Std_Name = stp.Std_Name;
                //mypro.Std_RollNo = stp.Std_RollNo;
                //mypro.Std_Email = stp.Std_Email;
                //   mylist.Add(mypro);
            }



            //mystu_list.Add(std);

            // global.Gid_T = getcateId;
            ViewBag.id = getcateId;
            //   ViewData["data"] =  mystu_list;




            return View();
        }
        public ActionResult ShowStudents()
        {
            var qer1 = from stn in db.Students
                       where global.pid_admin == stn.GId
                       select stn;
            ViewData["studnt"] = qer1.ToList();
            return View();
        }
        public ActionResult RequestList()
        {
            var q2 = from g1 in db.Groups
                     select g1;
            foreach (var gquery in q2)
            {
                Group gg = new Group();
                gg.GId = gquery.GId;
                global.pplist.Add(gg);
            }
            /*  var q3 = from s in db.Students
                       where global.pid_admin == s.GId
                       select s;
              ViewData["stdata"] = q3.ToList();
              /* var req = from pj in db.Projects
                         select new { pj.Title };
               foreach (var gquery in req)
               {
                   Project pp = new Project();
                   pp.Title = gquery.Title;
                   global.reqList.Add(pp);
            
               }
               */
            return View();
        }
        public ActionResult SubmitChanges(FormCollection m)
        {
            string sts = m["chk_stats"];
            var q4 = from ch in db.Projects
                     from grps in db.Groups
                     where global.pid_admin == grps.GId && grps.PId == ch.PId
                     select ch;
            foreach (var stp2 in q4)
            {
                if (sts != null)
                {
                    stp2.Status = "RESERVED";
                }
            }
            db.SaveChanges();

            return View("RequestList");
        }
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult ProjectList()
        {

            var mylist = new List<Plist>();
            try
            {

                var q1 = from s1 in db.Students
                         from g1 in db.Groups
                         from p1 in db.Projects
                         where s1.GId == g1.GId && g1.PId == p1.PId

                         select new { p1.PId, p1.Status, p1.Title, p1.AdvisorName, s1.Std_Name, s1.Std_RegId, s1.Std_Email };

                foreach (var stp in q1)
                {
                    Plist mypro = new Plist();
                    mypro.projId = stp.PId;
                    mypro.Title = stp.Title;
                    mypro.AdvisorName = stp.AdvisorName;
                    mypro.Std_Name = stp.Std_Name;
                    mypro.status = stp.Status;
                    mypro.Std_RollNo = stp.Std_RegId;
                    mypro.Std_Email = stp.Std_Email;
                    //  mylist.Add(stp.Title);

                    //mylist.Add(stp.AdvisorName);
                    //mylist.Add(stp.Std_Name);
                    //mylist.Add(stp.Std_RollNo);
                    //   mylist.Add(stp.Std_Email);

                    mylist.Add(mypro);
                }

                ViewData["data"] = mylist;


                //ViewBag.ListProject = mylist;


            }
            catch (Exception e)
            {
                Console.WriteLine(e.InnerException);
            }
            return View();
        }





        public Student GetStudent(int id)
        {
            Student stu = db.Students.Find(id);
            return stu;

        }
        public ActionResult Edit(int id)
        {
            return View(GetStudent(id));
        }
        public void EditProduct(Student EditedProduct)
        {

            try
            {
                Student ExistingProuct = db.Students.SingleOrDefault(p => p.SId == EditedProduct.SId);
                db.Students.Attach(ExistingProuct);

                db.Entry(ExistingProuct).CurrentValues.SetValues(EditedProduct);
                ExistingProuct.GId = EditedProduct.GId;

                db.SaveChanges();
            }
            catch(Exception e)
            {
                Console.WriteLine(e.InnerException);
            }
        }
        [HttpPost]
        public ActionResult Edit(Student EditedProduct)
        {

            EditProduct(EditedProduct);
            return RedirectToAction("ViewList");
        }
        //edit project

        public Project GetProject(int id)
        {
            Project projo = db.Projects.Find(id);
            return projo;

        }


        public ActionResult EditProject(int id)
        {
            return View(GetProject(id));
        }
        public void EditProj(Project projt)
        {
            //Models.MVCBlogDBEntities entities = new MVCBlogDBEntities();
            Project ExistingProuct = db.Projects.SingleOrDefault(p => p.PId == projt.PId);
            db.Projects.Attach(ExistingProuct);

            db.Entry(ExistingProuct).CurrentValues.SetValues(projt);
            ExistingProuct.PId = projt.PId;
            db.SaveChanges();
        }

        [HttpPost]
        public ActionResult EditProject(Project Projt, FormCollection c)
        {
            string research = c["chk1"];
            string development = c["chk2"];
            // bool de=bool.Parse(development);
            string objectoriented = c["chk3"];
            //bool or = bool.Parse(objectoriented);
            string structured = c["chk4"];

            string status = c["chk_stats"];
            string webbased = c["chk5"];
            string distributed = c["chk6"];
            string desktopbased = c["chk7"];
            string setupConfiguartion = c["chk8"];
            if (c["chk1"] != null)
            {
                Projt.Type = "research";
            }
            if (development != null)
            {
                Projt.Type = "development";
            }
            if (objectoriented != null)
            {
                Projt.Dev_Technology = "object oriented";
            }
            if (structured != null)
            {
                Projt.Dev_Technology = "structured";
            }
            if (webbased != null)
            {
                Projt.Plateform = "Web Based";
            }
            if (distributed != null)
            {
                Projt.Plateform = "Distributed";
            }
            if (desktopbased != null)
            {
                Projt.Plateform = "Desktop Based";
            }
            if (setupConfiguartion != null)
            {
                Projt.Plateform = "Setup Configuration";
            }
            if (status != null)
            {
                Projt.Status = "pending";
            }
            EditProj(Projt);

            return RedirectToAction("ProjectList");
        }


        public ActionResult Delete(int id = 0)
        {
            Student stu = db.Students.Find(id);
            if (stu == null)
            {
                return HttpNotFound();
            }
            return View(stu);
        }

        //
        // POST: /Home/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {
                Student stu = db.Students.Find(id);
                db.Students.Remove(stu);
                db.SaveChanges();
                return RedirectToAction("ViewList");
        
            }
            catch (Exception e)
            {

                Console.WriteLine(e.InnerException);
        
            }

            return View("ViewList");
        }

        public ActionResult Create()
        {
            return View();
        }
        public ActionResult ViewList()
        {
            var userName = WebSecurity.CurrentUserName;

            var quer = from stnt in db.Students
                       where stnt.Std_Email == userName
                       select stnt;
            ViewData["lst"] = quer.ToList();
            return View();
            // return View(db.Students.ToList());
        }
        //[AcceptVerbs(HttpVerbs.Post)]
        [HttpPost]
        public ActionResult SendRequest(FormCollection m)
        {
            FYPEntities5 db = new FYPEntities5();
            try
            {
                string mycount = Request.Form["count"];
                int totallenght = mycount.Length;
                string s = mycount[totallenght - 1].ToString();
                int c1 = int.Parse(s);
                Project pro = new Project();
                string title = m["projTitle"];
                global.projecttitle = title;
                string Ad_name = m["projAdvisor"];
                string description = m["Des"];
                string ad_email = m["projAdvisorEmail"];
                pro.Title = title;
                pro.AdvisorName = Ad_name;
                pro.Description = description;
                pro.AdvisorEmail = ad_email;

                string research = m["chk1"];

                string development = m["chk2"];
                // bool de=bool.Parse(development);
                string objectoriented = m["chk3"];
                //bool or = bool.Parse(objectoriented);
                string structured = m["chk4"];
                //bool structer = bool.Parse(structured);
                string webbased = m["chk5"];
                string distributed = m["chk6"];
                string desktopbased = m["chk7"];
                string setupConfiguartion = m["chk8"];
                string status = m["chk_sts"];
                if (research != null)
                {
                    pro.Type = "research";
                }
                if (development != null)
                {
                    pro.Type = "development";
                }
                if (objectoriented != null)
                {
                    pro.Dev_Technology = "object oriented";
                }
                if (structured != null)
                {
                    pro.Dev_Technology = "structured";
                }
                if (webbased != null)
                {
                    pro.Plateform = "Web Based";
                }
                if (distributed != null)
                {
                    pro.Plateform = "Distributed";
                }
                if (desktopbased != null)
                {
                    pro.Plateform = "Desktop Based";
                }
                if (setupConfiguartion != null)
                {
                    pro.Plateform = "Setup Configuration";
                }
                if (status != null)
                {
                    pro.Status = "pending";
                }
                db.Projects.Add(pro);
                db.SaveChanges();


                Group grp = new Group();
                grp.PId = pro.PId;
                db.Groups.Add(grp);
                db.SaveChanges();
              /*  Student st = new Student();
                string name = m["Name"];
                string Reg_Id = m["Id"];
                string degree=Reg_Id.Substring(0, 2);
                string Email = m["Email"];
                if (name != null)
                {
                    st.Std_Name = name;
                }
                //db.MyUsers.AddObject(us);()
                if (Reg_Id != null)
                {
                    st.Std_RegId = Reg_Id;
                    st.Degree = degree;

                }
                if (Email != null)
                {
                    st.Std_Email = Email;
                }
                st.GId = grp.GId;
                db.Students.Add(st);
                db.SaveChanges();

                Student st1 = new Student();

                string names = m["Names"];
                string Reg_Ids = m["Ids"];
                string Emails = m["Emails"];
                if (names != null)
                {
                    st1.Std_Name = names;
                }
                if (Reg_Ids != null)
                {

                    st1.Std_RegId = Reg_Ids;
                }
                if (Emails != null)
                {
                    st1.Std_Email = Emails;
                }
                st1.GId = grp.GId;

                //db.MyUsers.AddObject(us);


                db.Students.Add(st1);
                db.SaveChanges();
                */
                for (int i = 0; i < c1; i++)
                {
                    Student st2 = new Student();
                    if (i == 0)
                    {
                        st2.Std_RegId = Request.Form["Id1"];
                        st2.Std_Name = Request.Form["Name1"];
                        st2.Degree = Request.Form["Id1"].Substring(0, 3);
                        st2.Std_Email = Request.Form["Email1"];

                        //st2.Std_Name = Request.Form["Name"+i.ToString()];
                        // st2.Std_RollNo = Request.Form["id" + i.ToString()];
                        //st2.Std_Email = Request.Form["Email" + i.ToString()];
                    }
                    if (i == 1)
                    {
                        st2.Std_RegId = Request.Form["Id2"];
                        st2.Std_Name = Request.Form["Name2"];
                        st2.Degree = Request.Form["Id2"].Substring(0, 3);

                        st2.Std_Email = Request.Form["Email2"];

                    }
                    if (i == 2)
                    {
                        st2.Std_RegId = Request.Form["Id3"];
                        st2.Std_Name = Request.Form["Name3"];
                        st2.Degree = Request.Form["Id3"].Substring(0, 3);

                        st2.Std_Email = Request.Form["Email3"];

                    }
                    if (i == 3)
                    {
                        st2.Std_RegId = Request.Form["Id4"];
                        st2.Std_Name = Request.Form["Name4"];
                        st2.Degree = Request.Form["Id4"].Substring(0, 3);

                        st2.Std_Email = Request.Form["Email4"];

                    }

                    if (i == 4)
                    {
                        st2.Std_RegId = Request.Form["Id5"];
                        st2.Std_Name = Request.Form["Name5"];
                        st2.Degree = Request.Form["Id5"].Substring(0, 3);

                        st2.Std_Email = Request.Form["Email5"];

                    }

                    st2.GId = grp.GId;
                    db.Students.Add(st2);
                    db.SaveChanges();
                }
                /* string name1 = m["Name1"];
                 string Reg_Id1 =m["Id1"];
                 string Email1 = m["Email1"];
                 if (name1 != null)
                 {
                     st2.Std_Name = name1;
                 }
                 if (Reg_Id1 != null)
                 {
                     //db.MyUsers.AddObject(us);
                     st2.Std_RollNo = Reg_Id1;
                 }
                 if (Email1 != null)
                 {
                     st2.Std_Email = Email1;
                 }
                 if (name1 != null || Reg_Id1 != null || Email1 != null)
                 {
                     st2.GId = grp.GId;
                 }
                 if (name1 == null || Reg_Id1 == null || Email1 == null)
                 {
                     st2.GId = ' ';
                 }
           
                 db.Students.Add(st2);
                 db.SaveChanges();
                 Student st3 = new Student();
                 string name2 = m["Name2"];
                 string Reg_Id2 = m["Id2"];
                 string Email2 = m["Email2"];
                 if (name2 != null)
                 {
                     st3.Std_Name = name2;
                 }
                 if (Reg_Id2 != null)
                 {
                     //db.MyUsers.AddObject(us);
                     st3.Std_RollNo = Reg_Id2;
                 }
                 if (Email2 != null)
                 {
                     st3.Std_Email = Email2;
                 }
                 if (name2 != null || Reg_Id2 != null || Email2 != null)
                 {
                     st3.GId = grp.GId;
                 }
                 if (name2 == null || Reg_Id2 == null || Email2 == null)
                 {
                     st3.GId = ' ';
                 }
                 //db.MyUsers.AddObject(us);


                 db.Students.Add(st3);
                 db.SaveChanges();

                 Student st4 = new Student();
                 string name3 = m["Name3"];
                 string Reg_Id3 = m["Id3"];
                 string Email3= m["Email3"];
                 if (name3 != null)
                 {
                     st4.Std_Name = name3;
                 }
                 if (Reg_Id3 != null)
                 {
                     //db.MyUsers.AddObject(us);
                     st4.Std_RollNo = Reg_Id3;
                 }
                 if (Email3 != null)
                 {
                     st4.Std_Email = Email3;
                 }
                 if (name3 != null || Reg_Id3 != null || Email3 != null)
                 {
                     st4.GId = grp.GId;
                 }
                 if (name3 == null && Reg_Id3 == null && Email3 == null)
                 {
                     st4.GId = ' ';
                 }
                 //db.MyUsers.AddObject(us);
                 */
            }
            catch (Exception e)
            {
                Console.WriteLine(e.InnerException);
            }
            return View("StudentView");
        }




    }
}
